﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

using eSYSU.Business;


public partial class Manage_NewsEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            NewsCategoryInit(ddl_category);
        }
    }

    protected void NewsCategoryInit(DropDownList DropDownListName)
    {
        DropDownListName.DataSource = CollegeService.GetColleges(0);
        DropDownListName.DataTextField = "Name";
        DropDownListName.DataValueField = "ID";
        DropDownListName.DataBind();
    }
  
    protected void AddNews()
    {
        DBO dbo = new DBO();
        MySqlParameter[] msp_News = { 
               new MySqlParameter("@CollegeID",MySqlDbType.Int32),
               new MySqlParameter("@Title",MySqlDbType.VarChar,255,title.Text),
               new MySqlParameter("@Author",MySqlDbType.VarChar,63,author.Text),
               new MySqlParameter("@PostTime",MySqlDbType.DateTime),
               new MySqlParameter("@EmbodyTime",MySqlDbType.DateTime), 
               new MySqlParameter("@Origin",MySqlDbType.VarChar,255,origin.Text), 
               new MySqlParameter("@Brief",MySqlDbType.Text), 
               new MySqlParameter("@Content",MySqlDbType.LongText), 
               new MySqlParameter("@Keyword",MySqlDbType.VarChar,63,keyword.Text),
               new MySqlParameter("@OriginalURL",MySqlDbType.Text)
           };

        msp_News[0].Value = ddl_category.SelectedItem.Value;
        msp_News[1].Value = title.Text;
        msp_News[2].Value = author.Text;
        msp_News[3].Value = Convert.ToDateTime(posttime.Text);
        msp_News[4].Value = DateTime.Now;
        msp_News[5].Value = origin.Text;
        msp_News[6].Value = brief.Text;
        msp_News[7].Value = eSYSU.Common.ImageHelper.HtmlRemoteImageHandler(contents.Text);
        msp_News[8].Value = keyword.Text;
        msp_News[9].Value = url.Text;

        dbo.ExecProcDirectly("proc_News_Insert", msp_News);
        //string sSql = "insert into news(CategoryID,Title,Author,PostTime,Origin,Content,Keyword,OriginalURL) values("+ddl_category.SelectedItem.Value+","
        //    + title.Text + ","+author.Text+","+DateTime.Now.ToLongDateString()+","
        //    +origin.Text+","+ doHtml(contents.Text)+","+keyword.Text+","+url.Text+")";
        //dbo.ExecSQLDirectly(sSql);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        AddNews();
    }

    public String doHtml(String str)
    {
        if (str == null)
            return string.Empty;
        else
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            char[] chs= str.ToCharArray();
            for (int i = 0; i < chs.Length; i++)
            {
                switch (chs[i])
                {
                    case '&': sb.Append("&"); break;
                    case '<': sb.Append("<"); break;
                    case '>': sb.Append(">"); break;
                    case '\n': sb.Append("<br>"); break;
                    case '\t': sb.Append("        "); break;
                    case ' ': sb.Append(" "); break;
                    case '\'': sb.Append("''"); break;
                    case '\\': sb.Append("\\\\"); break;
                    default: sb.Append(chs[i]); break;
                }
            }
            return sb.ToString();
        }
    }
}